/**
 * @desc 请求封装
 * @auth 韩
 */
import axios from "axios";


let baseUrl = process.env.REACT_APP_BASE_URL;

// 创建axios实例 ，设置默认请求配置
const instance = axios.create({
    timeout: 60000,//超时时间
    baseURL: baseUrl//基本路由
})

//统一设置post请求头
instance.defaults.headers.post['Content-Type'] = "application/json";
//请求拦截器
instance.interceptors.request.use(config => {
    return config;
}, error => {
    // console.log("error请求错误", error)
    return Promise.reject(error)
})


instance.interceptors.response.use(response => {
    // console.log("返回拦截：",response);
    if (response.status === 200) {
        return Promise.resolve(response.data)
    } else {
        return Promise.reject(response.data.responseMsg)
    }
}, error => {
    if (error.response) {
        if (error.response.status === 401) {
        }
        return Promise.reject(error)
    } else {
        return Promise.reject("请求超时，请重试");
    }
})

// 封装get请求
const get = (url, params, config = {}) => {
    return new Promise((resolve, reject) => {
        instance({
            method: "get",
            url,
            params,
            ...config
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

//封装post请求
const post = (url, data, config = {}) => {
    return new Promise((resolve, reject) => {
        instance({
            method: 'post',
            url,
            data,
            ...config
        }).then(response => {
            resolve(response)
        }).catch((error) => {
            reject(error)
        })
    })
}

// 封装二进制流formData上传
const upload = ({
    url = String,
    params = Object,
    formData = null,
    headers = Object,
}) => {

    return new Promise((resove, reject) => {
        instance({
            method: 'post',
            url,
            params: { ...params },
            data: formData,
            headers: {
                "Content-Type": "multipart/form-data",
                ...headers
            }
        }).then(response => {
            resove(response);
        }).catch(error => {
            reject(error)
        })
    })
}

// 封装http  upload上传，
const http = {
    upload: async ({
        url = String,
        params = Object,
        formData = null,
        success = Function,
        failure = Function,
        complete = Function
    }) => {
        try {
            const res = await upload({
                url,
                params,
                formData,

            })
            if (res.resCode === '0000') {
                success(res);
            } else {
                failure(res.responseMsg)
            }
        } catch (error) {
            failure('网络连接失败', error);
        } finally {
            complete()
        }
    },
    post: async ({
        url = String,
        params = Object,
        before = () => { },
        success = () => { },
        failure = () => { },
        complete = () => { }
    }) => {
        try {
            before();
            const res = await post(url, {
                ...params,
            })
            if (res.responseCode === "0000") {
                success(res)
            } else {
                failure(res.responseMsg)
            }
        } catch (error) {
            console.log(error)
            failure("网络连接失败")
        } finally {
            complete()
        }
    },
    get: async ({
        url = String,
        params = Object,
        before = Function,
        success = Function,
        failure = Function,
        complete = Function
    }) => {
        before()
        try {

            const res = await get(url, {
                ...params,
            })

            if (res.responseCode === "0000") {
                success(res)
            } else {
                failure(res.responseMsg)
            }
        } catch (error) {
            console.log(error)
            failure("网络连接失败")
        } finally {
            complete()
        }
    },

}
export default http;